/**
 * This class represents a product
 * 
 * @author Konrad Paumann
 * @author Bernhard Denner
 * @author Maximilian Csuk
 * 
 * $Revision: 52 $ 
 * $Date: 2007-10-17 21:22:30 +0000 (Wed, 17 Oct 2007) $
 * 
 */

public class Product {
	/**
	 * variables
	 */
	protected String shortDescription;
	protected String description;
	protected int price;
	protected int amount;

	/**
	 * checks, if a price is valid
	 * 
	 * @param price
	 * @return price valid?
	 */
	protected boolean priceValid(int price) {
		return price > 0;
	}

	/**
	 * checks, if the product's price is valid
	 * 
	 * @return price valid?
	 */
	protected boolean priceValid() {
		return priceValid(price);
	}

	/**
	 * checks, if an amount is valid
	 * 
	 * @param amount
	 * @return amount valid?
	 */
	protected boolean amountValid(int amount) {
		return amount >= 0;
	}

	/**
	 * checks, if the product's amount is valid
	 * 
	 * @return amount valid?
	 */
	protected boolean amountValid() {
		return amountValid(amount);
	}

	/**
	 * checks, if a short-description is valid
	 * 
	 * @param shortDescription
	 * @return short-description valid?
	 */
	protected boolean shortDescriptionValid(String shortDescription) {

		// check shortdescription with regular expressions
		return shortDescription.matches("[A-Z0-9 ]{10}");
	}

	/**
	 * checks, if the product's short-description is valid
	 * 
	 * @return shortDescription valid?
	 */
	protected boolean shortDescriptionValid() {
		return shortDescriptionValid(shortDescription);
	}

	/**
	 * constructor
	 * 
	 * @param shortDescription
	 *            unique short-description of the product (10 characters max)
	 * @param description
	 *            description of the product
	 * @param price
	 *            price in cent
	 * @param amount
	 *            the current amount
	 * @throws ProductException
	 */
	public Product(String shortDescription, String description, int price,
			int amount) throws ProductException {

		// check the short-description
		if (!shortDescriptionValid(shortDescription))
			throw new ProductException("Kurzbezeichnung ist nicht korrekt!");

		// check the price
		if (!priceValid(price))
			throw new ProductException("Preis ist nicht korrekt!");

		// check the amount
		if (!amountValid(amount))
			throw new ProductException("Stueckzahl ist nicht korrekt!");

		this.shortDescription = shortDescription;
		this.description = description;
		this.price = price;
		this.amount = amount;
	}

	/**
	 * returns the description
	 * 
	 * @return description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * set the description
	 * 
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * returns the short-description
	 * 
	 * @return shortDescription
	 */
	public String getShortDescription() {
		return shortDescription;
	}

	/**
	 * returns the amount
	 * 
	 * @return amount
	 */
	public int getAmount() {
		return amount;
	}

	/**
	 * sets the amount
	 * 
	 * @param amount
	 */
	public void setAmount(int amount) throws ProductException {
		if (!amountValid(amount))
			throw new ProductException("Stueckzahl nicht korrekt!");
		this.amount = amount;
	}

	/**
	 * returns the price
	 * 
	 * @return price
	 */
	public int getPrice() {
		return price;
	}

	/**
	 * sets the price
	 * 
	 * @param price
	 */
	public void setPrice(int price) throws ProductException {
		if (!priceValid(price))
			throw new ProductException("Verkaufspreis nicht korrekt!");
		this.price = price;
	}

	/**
	 * alters the amount
	 * 
	 * @param amount
	 */
	public void alterAmount(int amount) throws ProductException {
		if (!amountValid(this.amount + amount))
			throw new ProductException("Stueckzahl nicht korrekt!");
		this.amount += amount;
	}

	/**
	 * Print out the product-details
	 */
	public void printDetails() {
//		System.out.println(getShortDescription());
//		System.out.println(getDescription());
//		System.out.println(getPrice());
//		System.out.println(getAmount());
		System.out.println(getOutputHeader());
		System.out.println(this);
	}
	
	/**
	 * @return Build String with product-details
	 */
	public String toString() {
		return (String)getShortDescription() + "\t" + getPrice() + "\t" +
			getAmount() + "\t" + getDescription();
	}
	
	/**
	 * 
	 * @return String with colum descriptions for output
	 */
	public static String getOutputHeader() {
		return "Kurzbeschr.\tPreis\tMenge\tBeschreibung";
	}
	 
}
